# == Schema Information
# Schema version: 20090415123829
#
# Table name: users
#
#  id               :integer(4)      not null, primary key
#  name             :string(40)
#  login            :string(40)
#  salt             :string(40)
#  crypted_password :string(40)
#  email            :string(40)
#  forum_profile_id :integer(4)
#  created_at       :datetime
#  updated_at       :datetime
#

require "digest/sha1"

class User < ActiveRecord::Base
  
  has_one :forum_profile
  has_many :posts
  has_many :topics
  
  
  attr_accessor :password,:email
  attr_protected :crypted_password,:salt
  validates_presence_of :login
  validates_confirmation_of :password, :if => :password_required?, :message => "Password should match confirmation"
  validates_presence_of :password,:if => :password_required?
  validates_presence_of :password_confirmation, :if => :password_required?
  validates_uniqueness_of :login
  validates_uniqueness_of :email, :on => :create, :message => "Email has been taken!"
  validates_length_of :login, :within => 3..20
  validates_length_of :password, :within => 4..40, :if => :password_required?
  validates_length_of :password_confirmation, :within => 4..40, :if => :password_required?
  validates_format_of :email, :with => /^([^@\s]{1}+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :on => :create, :message=>"Invalid email address."
  
  before_save :encrypt_password
  after_save  :handle_after_save
  def handle_after_save
    p = create_forum_profile
    p.is_active = true
  end
  
  def authenticated? pass
    crypted_password == User.encrypt(pass,self.salt)
  end
    
  def remember_me
    self.remember_token_expires_at = 10.years.from_now
  end  


  def self.authenticate login,pass
    u = find_by_login(login)
    u && u.authenticated?(pass) ? u : nil
  end
  
  # 
  # def encrypt pass
  #   self.class.encrypt(pass,salt)
  # end
  # 
  
  def self.encrypt pass, salt
    Digest::SHA1.hexdigest("--#{salt}--#{pass}fansg--")
  end
  
  
  protected
  def password_required?
    crypted_password.blank? || !password.blank?
  end
  
  # before save functiion   => encrypt password from user virtual state password
  def encrypt_password
    return if password.blank?
    self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
    self.crypted_password = User.encrypt(password,self.salt)
  end
end

